package it.univaq.mwt.model.entity;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import javax.persistence.CascadeType;

import javax.persistence.FetchType;
import javax.persistence.GenerationType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;



/**
 * Session Bean implementation class Users
 */
@Entity

@NamedQueries({ 
	@NamedQuery(name="authenticate",query="SELECT u FROM Users u WHERE u.username=:username AND u.password =:password"),
	@NamedQuery(name="editUser",query="UPDATE Users u SET u.username=:username, u.password =:password, u.name=:name, u.email=:email, u.id_g=:id_group WHERE u.idUser=:id_user"),
	//@NamedQuery(name="deleteUser",query="DELETE FROM Users u WHERE u.idUser=:id_user"),
	@NamedQuery(name="isUser",query="SELECT u FROM Users u WHERE u.id_g=:id_group AND u.idUser=:id_user"),
	@NamedQuery(name="getUserByIdUSer",query="SELECT u FROM Users u WHERE u.idUser=:id_user"),
	@NamedQuery(name="searchUser", query="SELECT u FROM Users u WHERE u.name LIKE :user OR u.username LIKE :user OR u.email LIKE :user"),
	@NamedQuery(name="searchNumUser", query="SELECT COUNT(u) FROM Users u "),
	@NamedQuery(name="getListUsers", query="SELECT u FROM Users u ORDER BY u.idUser")
	//@NamedQuery(name="getListUsersFriend", query="SELECT u FROM Users u JOIN u.user_friend f WHERE f.idUser=:id_user")
	//@NamedQuery(name="editUserById",query="UPDATE Users u SET u.username=:username, u.password =:password, u.name=:name, u.email=:email WHERE u.idUser=:id_user")
	
})
@SequenceGenerator(name="sequence",sequenceName="MASTER.USERS_SEQ")
@Table(name="Users")
public class Users implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Id
	@Column(name="id_user")
	@GeneratedValue(generator="sequence")
    
   	private int idUser;
	
	@Column(nullable=false,length=100)
	private String username;
	
	@Column(nullable=false,length=20)
	private String password;
	
	@Column(nullable=false,length=100)
	private String name;
	
	@Column(nullable=false,length=100)
	private String email;
	
	@Lob
	@Column(nullable=true)
	private String description;
	
	@Column(nullable=true,length=100)
	private String birthdate;
	
	@Column(nullable=false, insertable=false, updatable=false)
	private int id_g;
	
	
	
	@ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH})
	  @JoinColumn(name="id_g", referencedColumnName="id_group")
	private Groups groups;
	
	/*@OneToOne(cascade={ CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH},fetch=FetchType.LAZY,optional=true)
	@JoinTable(name="user_list",
			joinColumns = {@JoinColumn(name="id_user", referencedColumnName="id_user", nullable=true)},
			inverseJoinColumns={@JoinColumn(name="id_list", referencedColumnName="id_list", nullable=true)})*/
	@OneToOne(cascade={ CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH})
	@JoinColumn(name="id_list", referencedColumnName="id_list")
	private Lists list;

	@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.REFRESH},fetch=FetchType.EAGER)
	@JoinTable(name="user_friend",
			joinColumns = {@JoinColumn(name="id_user", referencedColumnName="id_user", nullable=true)},
			inverseJoinColumns={@JoinColumn(name="id_friend", referencedColumnName="id_user", nullable=true)})
	private Set<Users> user_friend;
	
	
	
	public Set<Users> getUser_friend() {
	return user_friend;
}

public void setUser_friend(Set<Users> user_friend) {
	this.user_friend = user_friend;
}

	@ManyToMany(mappedBy="user_friend")	
	private Set<Users> users;
	
	
	public Set<Users> getUsers() {
		return users;
	}

	public void setUsers(Set<Users> users) {
		this.users = users;
	}

	/******FINE PROVA RELAZIONE SU STESSA TABELLA*********/		
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}


	
	public String getBirthdate() {
		return birthdate;
	}

	public void setBirthdate(String birthdate) {
		this.birthdate = birthdate;
	}

	public Lists getList() {
		return list;
	}

	public void setList(Lists list) {
		this.list = list;
	}

	public void setGroups(Groups groups) {
		this.groups = groups;
	}	
	
	public int getId_group() {
		return id_g;
	}
	public void setId_group(int id_group) {
		this.id_g = id_group;
	}
	public Groups getGroups() {
		return groups;
	}

	
	public void setIdUser(int idUser) {
		this.idUser = idUser;
	}
			
	public int getIdUser() {
		return idUser;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}



	
	


}
